package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.Dao;
import com.mohe.dangjian.model.LXGeneralParty;
import com.mohe.dangjian.service.interfaces.ILXGeneralParty;
import com.mohe.dangjian.utils.Page;

public class LXGeneralPartyImpl implements ILXGeneralParty {
	
	private Dao<LXGeneralParty> lxGeneralPartyDao;

	public Dao<LXGeneralParty> getLxGeneralPartyDao() {
		return lxGeneralPartyDao;
	}

	public void setLxGeneralPartyDao(Dao<LXGeneralParty> lxGeneralPartyDao) {
		this.lxGeneralPartyDao = lxGeneralPartyDao;
	}

	@Override
	public List<LXGeneralParty> getLXGeneralPartyList(
			LXGeneralParty lxGeneralParty, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from LXGeneralParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(lxGeneralParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + lxGeneralParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(lxGeneralParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + lxGeneralParty.getAddress() + "%%");
		}
		// 单位性质
		if (!StringUtils.isBlank(lxGeneralParty.getType())) {
			where.append(" and type like ?");
			params.add("%%" + lxGeneralParty.getType() + "%%");
		}
		// 单位员工数
		if (lxGeneralParty.getWorkerNum() != null) {
			where.append(" and workerNum = ?");
			params.add(lxGeneralParty.getWorkerNum());
		}
		// 单位负责人姓名
		if (!StringUtils.isBlank(lxGeneralParty.getLeaderName())) {
			where.append(" and leaderName like ?");
			params.add("%%" + lxGeneralParty.getLeaderName() + "%%");
		}
		// 负责人党内职务
		if (!StringUtils.isBlank(lxGeneralParty.getLeaderTitle())) {
			where.append(" and leaderTitle like ?");
			params.add("%%" + lxGeneralParty.getLeaderTitle() + "%%");
		}
		// 负责人电话
		if (!StringUtils.isBlank(lxGeneralParty.getLeaderTel())) {
			where.append(" and leaderTel like ?");
			params.add("%%" + lxGeneralParty.getLeaderTel() + "%%");
		}
		// 联系人
		if (!StringUtils.isBlank(lxGeneralParty.getContacts())) {
			where.append(" and contacts like ?");
			params.add("%%" + lxGeneralParty.getContacts() + "%%");
		}
		// 联系人电话
		if (!StringUtils.isBlank(lxGeneralParty.getContactsTel())) {
			where.append(" and contactsTel like ?");
			params.add("%%" + lxGeneralParty.getContactsTel() + "%%");
		}
		// 传真
		if (!StringUtils.isBlank(lxGeneralParty.getFax())) {
			where.append(" and fax like ?");
			params.add("%%" + lxGeneralParty.getFax() + "%%");
		}
		// 邮编
		if (!StringUtils.isBlank(lxGeneralParty.getZip())) {
			where.append(" and zip like ?");
			params.add("%%" + lxGeneralParty.getZip() + "%%");
		}

		where.append(" order by createDate desc");
		List<LXGeneralParty> list = lxGeneralPartyDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(LXGeneralParty lxGeneralParty) {
		StringBuffer where = new StringBuffer();
		where.append("from LXGeneralParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(lxGeneralParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + lxGeneralParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(lxGeneralParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + lxGeneralParty.getAddress() + "%%");
		}
		// 单位性质
		if (!StringUtils.isBlank(lxGeneralParty.getType())) {
			where.append(" and type like ?");
			params.add("%%" + lxGeneralParty.getType() + "%%");
		}
		// 单位员工数
		if (lxGeneralParty.getWorkerNum() != null) {
			where.append(" and workerNum = ?");
			params.add(lxGeneralParty.getWorkerNum());
		}
		// 单位负责人姓名
		if (!StringUtils.isBlank(lxGeneralParty.getLeaderName())) {
			where.append(" and leaderName like ?");
			params.add("%%" + lxGeneralParty.getLeaderName() + "%%");
		}
		// 负责人党内职务
		if (!StringUtils.isBlank(lxGeneralParty.getLeaderTitle())) {
			where.append(" and leaderTitle like ?");
			params.add("%%" + lxGeneralParty.getLeaderTitle() + "%%");
		}
		// 负责人电话
		if (!StringUtils.isBlank(lxGeneralParty.getLeaderTel())) {
			where.append(" and leaderTel like ?");
			params.add("%%" + lxGeneralParty.getLeaderTel() + "%%");
		}
		// 联系人
		if (!StringUtils.isBlank(lxGeneralParty.getContacts())) {
			where.append(" and contacts like ?");
			params.add("%%" + lxGeneralParty.getContacts() + "%%");
		}
		// 联系人电话
		if (!StringUtils.isBlank(lxGeneralParty.getContactsTel())) {
			where.append(" and contactsTel like ?");
			params.add("%%" + lxGeneralParty.getContactsTel() + "%%");
		}
		// 传真
		if (!StringUtils.isBlank(lxGeneralParty.getFax())) {
			where.append(" and fax like ?");
			params.add("%%" + lxGeneralParty.getFax() + "%%");
		}
		// 邮编
		if (!StringUtils.isBlank(lxGeneralParty.getZip())) {
			where.append(" and zip like ?");
			params.add("%%" + lxGeneralParty.getZip() + "%%");
		}

		return lxGeneralPartyDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveLXGeneralParty(LXGeneralParty lxGeneralParty) {
		return lxGeneralPartyDao.insert(lxGeneralParty);
	}

	@Override
	public boolean saveLXGeneralPartyList(
			List<LXGeneralParty> lxGeneralPartyList) {
		return lxGeneralPartyDao.insert(lxGeneralPartyList);
	}

	@Override
	public LXGeneralParty getLXGeneralPartyById(Long timeKey) {
		LXGeneralParty lxGeneralParty = lxGeneralPartyDao.getByKey(timeKey);
		if (lxGeneralParty != null) {
			return lxGeneralParty;
		}
		return null;
	}

	@Override
	public boolean updateLXGeneralParty(LXGeneralParty lxGeneralParty) {
		return lxGeneralPartyDao.update(lxGeneralParty);
	}

	@Override
	public boolean delLXGeneralParty(Long timeKey) {
		return lxGeneralPartyDao.delete(timeKey);
	}

	@Override
	public List<LXGeneralParty> getAllLXGeneralParty() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return lxGeneralPartyDao.query(where.toString(), null);
	}
}
